let app=getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone:"",
        snsMsgWait: 60,
        sendTime:'获取验证码',
        smsSend:false,
        bind:false,
        inputCode:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    
      this.setData({
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop
          })
    },
editPhone:function(){
    this.setData({
        bind:false,
        nowPhone:'',
    })
},
goBack:function(){
    wx.navigateBack();
  },

 getPhoneNumber (e){
    let that=this;
    var open=wx.getStorageSync('open');
     
    if(open == ''){
        wx.navigateTo({
            url: '../login/index',
          })
    }
    if(open == undefined){
        wx.navigateTo({
          url: '../login/index',
        })
    }
     console.log(e);   
            wx.request({
                url:app.globalData.domain+'/wechat/login/getPhone',
                data:{
                  code:e.detail.code,
                   id:open.id,
                   type:3
                },
                method:'GET',
                header:{
                    'content-type':'application/json'
                },
                success:function(res){
                  console.log(res)
                    //wx.setStorageSync('PhoneNumber',res.data.phone);
                    
                    if(res.statusCode==200){
                        var data = res.data;
                      open.phone = data.phone;
                      wx.setStorageSync('open', data.open);
                      wx.setStorageSync('user', data.user);
                      that.setData({
                          open:open,
                          login:false,
                          phone:res.data.phone
                      })
                      that.showModals('获取手机号成功');
                    }else{
                        that.showModals('获取手机号失败');
                    } 
                    //that.show();
                    //console.log("手机号为");
                    //console.log(res);
                },
                fail:function(err){
                    console.log(err);
                }
            })
   
  },
  showModals:function(msg){
    wx.showModal({
        title: '系统提示',
        content:msg,
        success:function(res){
            return false;
        }
    })
},
 bindPhone:function(e){
     let that=this;
   var data=this.data;
   var code=data.code;
   var inputCode=data.inputCode;
   var phone=data.nowPhone;
   var open=wx.getStorageSync('open');

   if (!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(phone))) {
    wx.showToast({
      title: '手机号输入错误',
      icon: 'none'
    })
    return;
  }
   if(inputCode==0){
     wx.showToast({
       title: '请输入验证码',
     })
     return false;
   }
var reg = /^\d{4}$/;
//var str = "0001";
   if(reg.test(inputCode)){
     wx.request({
       url: app.globalData.domain+'mine/bind',
       data:{user_id:open.id,phone:phone,from:5,inputCode:inputCode},
       success:function(res){
         console.log(res);
         if(res.data.state==200){
           var open=res.data.list;
           wx.setStorageSync('open',open);
           wx.showModal({
             title: '提示信息',
             showCancel:false,
             content:'绑定成功',
             success:function(e){
                 that.setData({
                     bind:true
                 })
               if(e.confirm){
                var pages = getCurrentPages();
                //var beforePage = pages[pages.length - 2];
                var currentPage = pages[pages.length - 1];
                var beforePage = pages[pages.length - 2];
                if(beforePage){
                  beforePage.onLoad();
                }
                //beforePage.loadData();
                wx.navigateBack({
                  delta: 1,
                })       
               }
 
             }
           })
         }
         if(res.data.state==301){
          wx.showToast({
            title: '电话号码为空',
          })
         //wx.navigateBack();
      }
         if(res.data.state==302){
             wx.showToast({
               title: '电话号码没有改变',
             })
            //wx.navigateBack();
         }
         if(res.data.state==303){
          wx.showToast({
            title: '验证码错误！',
          })
         //wx.navigateBack();
      }
       }
     })
   }else{
     wx.showToast({
       title: '验证码输入错误',
     })
   }
 
 },
 
 inputCode:function(e){
   //console.log(e);
   var inputCode=e.detail.value;
   //if(thisd){}
   this.setData({
     inputCode:inputCode
   })
 
 },
  inputPhone:function(e){
    //console.log(e);
    var phone=e.detail.value;
    this.setData({
    nowPhone:phone
    })
 
  },
  goto(e){
    console.log(e);
    var url=e.currentTarget.dataset.url;
    console.log(url);
    var index=e.currentTarget.dataset.index;
    app.goto(url,index)
  // console.log(url);
},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      let that=this;
      var open=wx.getStorageSync('open');
      console.log(open);
      var phone=open.phone;
      if(phone){
          this.setData({
              phone:phone
          })
      }
   
  
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})